<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@page import="javax.portlet.*"%>
<%@page import="java.util.ArrayList" %>
<%@page import="com.jefe.bamboo.BambooProject" %>
<%@page import="com.liferay.portal.model.User" %>
<%@page import="com.liferay.portal.util.PortalUtil" %>
<%@page import="com.jefe.bamboo.service.service.*" %>
<%@page import="com.jefe.bamboo.service.model.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%--
Author: Sonali Mendis
Code:   Bamboo Portlet for Jefe (http://www.jefesolutions.com)
License:GNU
--%>
<portlet:defineObjects />
<%-- Get the User --%>
<%
Boolean userSet = false;
    long id = 0;
    String user = null;
    String host = null;
    int port = 0;
    User currentUser = null;
    try {
        currentUser = PortalUtil.getUser(request);
        id = currentUser.getUserId();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    try{
    UserPrefsBmb userDtls = UserPrefsBmbLocalServiceUtil.getUserPrefsBmb(id);
    user = userDtls.getBmbUser();
    host = userDtls.getBmbHost();
    port = userDtls.getBmbPort();
    userSet = true;
    }catch(Exception ex){
         user = "any user";
         System.out.println(ex.getMessage());
    }
%>
<%-- Checking whethere this is the first time --%>
<%
    PortletURL url = renderResponse.createActionURL();

    if(!userSet){ // this is the first time
        url.setParameter("edit", "link");
%>
        <%-- Give a link to edit --%>
        <a href="<%=url.toString()%>">Configure the Bamboo URL</a>
<% }else{ /*this is not the first time*/
        url.setParameter("edit", "form");
%>
    <%-- Provide the filter form --%>
    <form action="<%=url.toString()%>" method="post">
        <span class="portlet-form-label">Project Filter: &nbsp;</span>
        <input class="portlet-form-input-field" type="text" name="vformFilter" value="" />&nbsp;
        <input class="portlet-form-button" type="submit" value="Go" />
        <span><div class="portlet-font" style="font-size: xx-small">(Input the bamboo <strong>Project Key</strong> or <strong>Plan Key</strong> or <strong>Build Number</strong> of the build, as the 'Project Filter'. Eg: TEST-PORTLET-2 or TEST or PORTLET or 2)</div></span>
    </form>
    <%-- Provide the error msgs --%>
    <%
    String error = (String)renderRequest.getAttribute("error");
    if(error!=null){
        out.println("<div class=\"portlet-msg-error\">"+error+"</div>");
    }else{
    ArrayList<BambooProject> list =(ArrayList<BambooProject>)renderRequest.getAttribute("projList");
    %>
    <div class="portlet-msg-success" align="justify" style="text-align: justify">
        Build results of the projects authorized to <%=user%>, as on <b><%= new java.util.Date() %></b><br />
        <b><%=list.size() %></b> number of <b>matching results</b> found.
    </div>
    <%-- Display the refresh icon --%>
    <div align="right">
        <%

        %>
        <form action="<%=url.toString()%>" method="post">
            <input type="hidden" name="vformFilter" value="*"/>
            <input type="image" src="<%=request.getContextPath().toString() %>/images/refresh.png" title="Refresh" alt="To Refresh"/>
        </form>
    </div>
    <%-- Display the results --%>
    <div class="results-grid">
        <table class="taglib-search-iterator" >
                <tr class="portlet-section-header results-header">
                    <th class="col-1 "></th>
                    <th class="col-2">Last Build Status</th>
                    <th class="col-3">Job Name</th>
                </tr>
                <tr class="portlet-section-body results-row">
                    <td></td>
                    <td></td>
                </tr>
                <% for(int i = 0; i < list.size(); i++){%>
                    <tr class="portlet-section-body results-row">
                        <td class="col-1"><% out.println("<img src=\""+request.getContextPath().toString()+"/images/"+list.get(i).getLastBuildImage()+"\" title="+list.get(i).getLastBuildStatus()+" />"); %></td>
                        <td class="col-2"><% out.println(list.get(i).getLastBuildStatus()); %></td>
                        <td class="col-3"><a href="<% out.println(list.get(i).getProjectURL()); %>"><% out.println(list.get(i).getProjectName()); %></a></td>
                    </tr>
                <% } %>
        </table>
    </div>
    <% if(userSet){%>
        <%-- Footer to link to Hudson --%>
        <hr style="border-bottom: dotted 1px"/>
        <div class="portlet-section-footer" >
            More on: <a href="http://<%=host%>:<%=port%>" >Bamboo</a>
        </div>
    <%} %>
<% }} %>